package com.lza.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * jwt登陆授权过滤器
 */

public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    private String token;
    @Value("${jwt.header}")
    private String header;//实际就是Authorization 就是每次在请求头Authorization里面获取内容
    @Value(("${jwt.tokenHeader"))
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;  //实际是Bearer
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    UserDetailsService userDetailsService;
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String authHeader = httpServletRequest.getHeader(header);//获取header中的验证信息
        System.out.println("真正从请求头获取token:"+authHeader);
        System.out.println(authHeader);
        //存在token
        if(null!=authHeader&&authHeader.startsWith(tokenHead)){  //请求头上去除Bearer的操作，就是从Bearer开始
            System.out.println("存在token");
            String authToken = authHeader.substring(tokenHead.length());
            token=authToken;
            System.out.println("一顿操作后去除Bearer:"+token);
            String userName = jwtTokenUtil.getUserNameFromToken(authToken);
            //token存在用户名，但没有登录
            if(null!=userName&& null== SecurityContextHolder.getContext().getAuthentication()){
                System.out.println("token存在用户名，但没有登录");
                //登录
                UserDetails userDetails = userDetailsService.loadUserByUsername(userName);
                //验证token是否有效，重新设置用户对象
                if(jwtTokenUtil.validateToken(authToken, userDetails)){
                    System.out.println("验证token是否有效，重新设置用户对象");
                    UsernamePasswordAuthenticationToken authenticationToken=new
                            UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
                    authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
                    SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                }
            }
        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }
    //获取token
    public String getToken(){
        return token;
    }
}
